﻿<MudDialog>
    <DialogContent>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto">
            <MudGrid>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.DimensionCode" For="@(() => _editRequestDto.DimensionCode)" Label="维度编码"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.DimensionName" For="@(() => _editRequestDto.DimensionName)" Label="维度名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudSelect T="string" Value="_editRequestDto.DimensionType" For="@(() => _editRequestDto.DimensionType)" Label="维度类型"
                               ValueChanged="DimensionTypeChangedHandle"
                               Dense="true" Clearable AnchorOrigin="Origin.BottomCenter"
                               Variant="Variant.Outlined" Margin="Margin.Dense">
                        <MudSelectItem Value="@("database")">来源数据库</MudSelectItem>
                        <MudSelectItem Value="@("valuepair")">自定义键值对</MudSelectItem>
                    </MudSelect>
                </MudItem>

                <MudDivider></MudDivider>

                @if (dataSourceType.Equals("database"))
                {
                    <MudItem xs="12">
                        <MudField Label="SQL语句" Variant="Variant.Outlined" InnerPadding="false" Margin="Margin.Dense">
                            <StandaloneCodeEditor Id="my-editor-instance-id" ConstructionOptions="EditorConstructionOptions" CssClass="my-editor-class"/>
                        </MudField>
                    </MudItem>

                    <MudItem xs="6">
                        <MudSelect T="string" @bind-Value="_editRequestDto.TextField"
                                   For="@(() => _editRequestDto.TextField)" Label="键字段"
                                   Dense="true" AnchorOrigin="Origin.BottomCenter"
                                   Variant="Variant.Outlined" Margin="Margin.Dense">
                            @*@foreach (var item in _dataTableCols)
                        {
                        <MudSelectItem Value="@(item.ColumnName)">@item.ColumnName</MudSelectItem>
                        }*@
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect T="string" @bind-Value="_editRequestDto.ValueField" For="@(() => _editRequestDto.ValueField)"
                                   Label="值字段" Dense="true" AnchorOrigin="Origin.BottomCenter"
                                   Variant="Variant.Outlined" Margin="Margin.Dense">
                            @*@foreach (var item in _dataTableCols)
                        {
                        <MudSelectItem Value="@(item.ColumnName)">@item.ColumnName</MudSelectItem>
                        }*@
                        </MudSelect>
                    </MudItem>
                }
                else
                {
                    if (_editRequestDto.Values != null)
                    {
                        foreach (var item in _editRequestDto.Values)
                        {
                            <MudItem xs="5">
                                <MudTextField @bind-Value="item.Text"
                                              For="@(() => item.Text)" Label="键字段"
                                              Variant="Variant.Outlined" Margin="Margin.Dense">
                                </MudTextField>
                            </MudItem>
                            <MudItem xs="5">
                                <MudTextField @bind-Value="item.Value"
                                              For="@(() => item.Value)" Label="值字段"
                                              Variant="Variant.Outlined" Margin="Margin.Dense">
                                </MudTextField>
                            </MudItem>
                            <MudItem xs="2">
                                <MudIconButton Color="Color.Error" Icon="@Icons.Material.Filled.Remove"
                                               OnClick="@(() => RemoveItemClickHandle(item))">
                                </MudIconButton>
                            </MudItem>
                        }
                    }
                    <MudItem xs="12">
                        <MudButton Variant="Variant.Filled" Size="Size.Small"
                                   StartIcon="@Icons.Material.Filled.Add" IconColor="Color.Primary"
                                   OnClick="AddItemClickHandle">
                            添加字段
                        </MudButton>
                    </MudItem>
                }

                <MudItem xs="12">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="@(() => _editRequestDto.Description)" Label="描述"
                                  Lines="3" Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>

            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle">保存</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle">取消</MudButton>
    </DialogActions>
</MudDialog>


@code {
    private MudForm _editRequestForm = new();
    private DimensionEditRequestDto _editRequestDto = new();
    private string dataSourceType = "database";

    private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
    {
        return new StandaloneEditorConstructionOptions
        {
            Language = "sql",
            GlyphMargin = true,
            Value = "",
            AutomaticLayout = true
        };
    }

    [CascadingParameter]
    public MudDialogInstance? MudDialog { get; set; }

    void CancelHandle()
    {
        if (MudDialog != null)
            MudDialog.Cancel();
    }

    async Task SubmitHandle()
    {
        await _editRequestForm.Validate();
    }

    Task DimensionTypeChangedHandle(string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            _editRequestDto.DimensionType = value;
            dataSourceType = value;
        }
        return Task.CompletedTask;
    }

    public void AddItemClickHandle()
    {
        if (_editRequestDto.Values != null &&
            _editRequestDto.Values.Any(i => string.IsNullOrEmpty(i.Text)))
        {
            return;
        }

        _editRequestDto.Values?.Add(new DimensionValuesEditDto());
    }

    public void RemoveItemClickHandle(DimensionValuesEditDto item)
    {
        _editRequestDto.Values?.Remove(item);
    }

}